package com.datawin.taotaocheche.ui.actviity.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jakewharton.rxbinding.view.RxView;
import com.datawin.taotaocheche.R;
import com.datawin.taotaocheche.base.BaseActivity;
import com.datawin.taotaocheche.bean.SettingInfo;
import com.datawin.taotaocheche.comm.Constants;
import com.datawin.taotaocheche.comm.MyApplication;
import com.datawin.taotaocheche.event.NetErrorEvent;
import com.datawin.taotaocheche.event.SelectCityEvent;
import com.datawin.taotaocheche.ui.actviity.login.biz.SelectInfoBiz;
import com.datawin.taotaocheche.ui.actviity.login.component.DaggerSetecltInfoComponent;
import com.datawin.taotaocheche.ui.actviity.login.contract.SelectInfoContract;
import com.datawin.taotaocheche.ui.actviity.login.module.SelectInfoModule;
import com.datawin.taotaocheche.ui.actviity.login.presenter.SelectInfoPresenter;
import com.datawin.taotaocheche.ui.widget.wheel.ChangeAddressDialog;
import com.datawin.taotaocheche.util.LogUtils;
import com.datawin.taotaocheche.util.PreferencesUtils;
import com.datawin.taotaocheche.util.StringUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import javax.inject.Inject;

import butterknife.Bind;
import rx.functions.Action1;

public class SelcetInfoAct extends BaseActivity implements SelectInfoContract.View {


    @Inject
    SelectInfoPresenter presenter;
    @Inject
    SelectInfoBiz bia;

    @Bind(R.id.select_info_city_text)
    TextView cityText;
    @Bind(R.id.select_info_city)
    LinearLayout city;
    @Bind(R.id.select_info_yaer_text)
    TextView yearText;
    @Bind(R.id.select_info_yaer)
    LinearLayout yaer;
    @Bind(R.id.select_info_mileage_text)
    TextView mileageText;
    @Bind(R.id.select_info_mileage)
    LinearLayout mileage;
    @Bind(R.id.select_info_type_text)
    TextView typeText;
    @Bind(R.id.select_info_type)
    LinearLayout typeLayout;
    @Bind(R.id.select_info_submit)
    TextView submit;

    private String[] Str = {"不限", "1年以下", "1-3年", "3-5年", "5年以上"};
    private List<String> carYear = new ArrayList<String>();
    private String[] Str1 = {"不限", "1万公里以下", "1-3万公里", "3-5万公里", "5万公里以上"};
    private List<String> carMel = new ArrayList<String>();

    private String[] Str2 = {"不限", "两厢轿车", "三厢轿车", "跑车", "SUV", "MPV", "面包车", "皮卡"};
    private List<String> carType = new ArrayList<String>();

    private ChangeAddressDialog carYearDialog;
    private ChangeAddressDialog carMelDialog;
    private ChangeAddressDialog carTypeDialog;
    SettingInfo settingInfo = new SettingInfo();
    String type = "";

    @Override
    protected void setRoot() {
        setTitleBar("#FF03A8D2");
        setContentView(R.layout.activity_select_info);
    }

    @Override
    protected void setup() {
        setToolBar("自定义筛选", "");
        DaggerSetecltInfoComponent.builder().selectInfoModule(new SelectInfoModule(this)).build().inject(this);
        presenter.setBiz(bia);
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        intUI();
        RxView.clicks(city)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        startActString(SelectCityAct.class, settingInfo.getCityIds());
                    }
                });
        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        presenter.toMian(settingInfo, type);
                    }
                });
        RxView.clicks(yaer)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setYearInfo();
                    }
                });
        RxView.clicks(mileage)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setRoadInfo();
                    }
                });
        RxView.clicks(typeLayout)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setTypeInfo();
                    }
                });


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCityEvent(SelectCityEvent info) {
        if (info.getCity().length() > 0) {
            settingInfo.setCityIds(info.getCity().substring(0, info.getCity().length() - 1));
            cityText.setText(info.getCity().substring(0, info.getCity().length() - 1));
        }else{
            cityText.setText("不限");
            settingInfo.setCityIds("");
        }

    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }


    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bia.onDestory();
    }

    private void setYearInfo() {
        carYearDialog = new ChangeAddressDialog(
                SelcetInfoAct.this, carYear, "请选择汽车年限");
        carYearDialog
                .setAddresskListener(new ChangeAddressDialog.OnAddressCListener() {
                    @Override
                    public void confirm(int position, String province) {
                        yearText.setText(carYear.get(position));
                        settingInfo.setYear(position);
                        carYearDialog.dismiss();
                        carYearDialog = null;
                    }

                    @Override
                    public void cancel() {
                        carYearDialog.dismiss();
                        carYearDialog = null;
                    }
                });
        carYearDialog.show();
    }


    private void setRoadInfo() {
        carMelDialog = new ChangeAddressDialog(
                SelcetInfoAct.this, carMel, "请选择汽车里程");
        carMelDialog
                .setAddresskListener(new ChangeAddressDialog.OnAddressCListener() {
                    @Override
                    public void confirm(int position, String province) {
                        mileageText.setText(carMel.get(position));
                        settingInfo.setRoad(position);
                        carMelDialog.dismiss();
                        carMelDialog = null;
                    }

                    @Override
                    public void cancel() {
                        carMelDialog.dismiss();
                        carMelDialog = null;
                    }
                });
        carMelDialog.show();
    }


    private void setTypeInfo() {
        carTypeDialog = new ChangeAddressDialog(
                SelcetInfoAct.this, carType, "请选择汽车车型");
        carTypeDialog
                .setAddresskListener(new ChangeAddressDialog.OnAddressCListener() {
                    @Override
                    public void confirm(int position, String province) {
                        typeText.setText(carType.get(position));
                        settingInfo.setCarType(position);
                        carTypeDialog.dismiss();
                        carTypeDialog = null;
                    }

                    @Override
                    public void cancel() {
                        carTypeDialog.dismiss();
                        carTypeDialog = null;
                    }
                });
        carTypeDialog.show();
    }


    private void intUI() {
        Constants.registerActivity.add(this);
        type = StringUtils.isEmpty(getIntent().getStringExtra(Constants.KEYSTRING)) ? "" : getIntent().getStringExtra(Constants.KEYSTRING);
        carYear = Arrays.asList(Str);
        carMel = Arrays.asList(Str1);
        carType = Arrays.asList(Str2);
        String setInfosStr = PreferencesUtils.getStringPreferences("carSettingInfo",
                "carSettingInfo", null);
        if (!TextUtils.isEmpty(setInfosStr)) {
            SettingInfo settingInfo = new Gson().fromJson(setInfosStr,
                    new TypeToken<SettingInfo>() {
                    }.getType());
            MyApplication.settingInfo = settingInfo;
        }

        if (MyApplication.settingInfo == null) {
            settingInfo.setCityIds("");
            settingInfo.setRoad(0);
            settingInfo.setYear(0);
            settingInfo.setCarType(0);
        } else {
            settingInfo.setCityIds(StringUtils.isEmpty(MyApplication.settingInfo.getCityIds()) ? "" : MyApplication.settingInfo.getCityIds());
            settingInfo.setRoad((MyApplication.settingInfo.getRoad() != null && Pattern.matches("^[0-5]", MyApplication.settingInfo.getRoad().toString())) ? MyApplication.settingInfo.getRoad() : 0);
            settingInfo.setYear((MyApplication.settingInfo.getYear() != null && Pattern.matches("^[0-5]", MyApplication.settingInfo.getYear().toString())) ? MyApplication.settingInfo.getYear() : 0);
            LogUtils.d(MyApplication.settingInfo.getCarType()+"");
            settingInfo.setCarType((MyApplication.settingInfo.getCarType() != null && Pattern.matches("^[0-8]", MyApplication.settingInfo.getCarType().toString())) ? MyApplication.settingInfo.getCarType() : 0);
        }

        if (!StringUtils.isEmpty(settingInfo.getCityIds())) {
            cityText.setText(settingInfo.getCityIds());
        }
        mileageText.setText(Str1[settingInfo.getRoad()]);
        yearText.setText(Str[settingInfo.getYear()]);
        typeText.setText(Str2[settingInfo.getCarType()]);

    }
}


